<template>
	<view class="wrap">
		<view class="box">
			<view class="b-row">
				<view class="b-tit">类型</view>
				<view class="b-cont f-option">
					<picker @change="onFilter" data-field="type" :value="typeIndex" :range="typeList" range-key="name">
						<view class="b-main">
							<view class="s-cont">{{this.typeList[this.typeIndex].name}}</view>
							<view class="iconfont icon-more"></view>
						</view>
					</picker>
				</view>
			</view>
			<view class="b-row">
				<view class="b-tit">内容</view>
				<view class="b-cont f-textarea">
					<textarea placeholder="请填写详细内容" auto-height @input="onInputText" data-field="content"></textarea>
				</view>
			</view>
			<view class="b-row">
				<view class="b-tit">图片</view>
				<view class="b-cont f-photos">
					<view v-if="form.photos.length > 0" class="s-photos">
						<view class="s-item" v-for="(item, index) in form.photos" :key="index">
							<image :src="item.url" mode="widthFix" @click="onPreview(index)"></image>
							<view class="s-del iconfont icon-wrong-b" @click="onDelete(index)"></view>
						</view>
					</view>
					<view class="s-upload iconfont icon-upload" @click="onUploadPhotos"></view>
				</view>
			</view>
		</view>
		<view class="btn-save" :class="{ disable: disable.submit }" @click="onSave">提交反馈</view>
		<view class="feedback" v-if="state.items.length > 0">
			<view class="b-title" v-if="state.items.length > 0">
				<view class="iconfont icon-line"></view>
				<view class="s-tit">我的反馈</view>
				<view class="iconfont icon-line"></view>
			</view>
			<view class="b-items">
				<view class="b-item" v-for="(item, index) in state.items" :key="index" @click="onJumpDetail(item.id)">
					<view class="b-main">
						<text class="s-cont too-long">{{item.content}}</text>
						<view class="s-more iconfont icon-more"></view>
					</view>
					<view class="b-foot">
						<view class="s-info">
							<view class="s-type">{{item.type_text}}</view>
							<view class="s-state" :class="'f-state-' + item.status">{{item.status_text}}</view>
						</view>
						<view class="s-time">{{timeFormat(item.created_at)}}</view>
					</view>
				</view>
			</view>
			<!-- 加载中 -->
			<load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
		</view>
	</view>
</template>

<script>
	import {
		uploadImage
	} from '@/service/api/common.js'
	import {
		submit,
		getRecordList
	} from '@/service/api/feedback.js'
	import {
		compressImage
	} from '@/common/util.js'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				form: {
					photos: [],
				},
				disable: {
					submit: false
				},
				state: {
					items: []
				},
				typeList: [{
					value: 1,
					name: '数据问题'
				}, {
					value: 2,
					name: '功能使用'
				}],
				typeIndex: 0,
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.onReset()
			this.loadRecordList(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadRecordList()
			}
		},
		methods: {
			// 输入内容
			onInputText(e) {
				console.log('onInputText', e)
				this.form[e.target.dataset.field] = e.detail.value;
			},
			onFilter(e) {
				console.log('onFilter', e.target.dataset.field, e.detail.value)
				const field = e.target.dataset.field
				if (field === 'type') {
					this.typeIndex = e.detail.value
					this.form.type = this.typeList[this.typeIndex].value
				}
			},
			onReset() {
				this.form = {
					type: 1,
					content: '',
					photos: [],
				}
			},
			onSave() {
				if (this.disable.submit) {
					return;
				}
				if (!this.form.content) {
					return this.$logic.showToast('内容不能为空')
				}
				this.disable.submit = true
				submit(this.form.type, this.form.content, this.form.photos).then(([err, res]) => {
					console.log('submit', err, res)
					this.disable.submit = false
					if (!err) {
						this.$logic.showToast('提交成功').then(([err, res]) => {
							this.onReset()
							this.loadRecordList(true)
						})
					}
				})
			},
			onUploadPhotos(e) {
				console.log('onUploadPhotos', e)
				uni.chooseImage({
					count: 9, // 1张
					sizeType: ['compressed'], // 压缩图
				}).then(([err, res]) => {
					console.log('chooseImage', err, res)
					if (!err) {
						let index = this.form.photos.length
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							compressImage(res.tempFiles[i], 1800, 2700).then(([err, res]) => {
								console.log('compressImage', err, res)
								if (!err) {
									uploadImage(res).then(([err, res]) => {
										console.log('uploadImage', err, res)
										if (!err) {
											this.form.photos[index++] = {
												url: res.fullurl
											}
										}
									})
								}
							})
						}
					}
				})
			},
			onPreview(index) {
				let photos = []
				for (let i = 0; i < this.form.photos.length; i++) {
					photos.push(this.form.photos[i].url)
				}
				uni.previewImage({
					current: index,
					urls: photos
				})
			},
			onDelete(index) {
				this.form.photos.splice(index, 1)
			},
			loadRecordList(refresh) {
				console.log('loadRecordList', refresh);
				if (refresh) {
					this.page = 1;
					this.state.items = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getRecordList(this.page, 10).then(([
					err, res
				]) => {
					console.log('getRecordList', err, res)
					this.loadingType = -1;
					if (!err) {
						if (res.items.length > 0) {
							this.state.items = [...this.state.items, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无数据'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onJumpDetail(id) {
				uni.navigateTo({
					url: '/pages/user/feedback_detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {
		padding: 0 40upx;
	}

	.box {
		margin-top: 30upx;
		background: #fff;
		border-radius: 20upx;

		.b-row {
			padding: 30upx;
			display: flex;
			justify-content: space-between;

			.b-tit {
				font-size: 28upx;
				width: 180upx;
				color: #666;
			}

			.b-cont {
				// flex: 1;
				display: flex;
				align-items: center;

				input {
					text-align: right;
				}

				textarea {
					text-align: right;
				}

				&.f-option {

					.b-main {
						display: flex;
						align-items: center;

						.s-cont {
							font-size: 28upx;
						}

						.iconfont {
							margin-left: 10upx;
						}
					}
				}

				&.f-input {
					input {
						width: 480upx;
					}
				}

				&.f-textarea {
					textarea {
						width: 530upx;
						font-size: 28upx;
					}
				}

				&.f-text {
					font-size: 28upx;
					color: #666;
				}


				&.f-photos {
					display: flex;
					align-items: inherit;

					.s-photos {
						width: 400upx;
						display: flex;
						flex-wrap: wrap;
						justify-content: flex-end;

						.s-item {
							margin-bottom: 20upx;
							margin-left: 20upx;
							position: relative;
							width: 180upx;

							image {
								width: 180upx;
							}

							.s-del {
								position: absolute;
								top: -15upx;
								right: -15upx;
								background: rgba(0, 0, 0, 0.5);
								width: 30upx;
								height: 30upx;
								border-radius: 15upx;
								color: #fff;
								font-size: 26upx;
								font-weight: bold;
								display: flex;
								align-items: center;
								justify-content: center;
							}
						}
					}

					.s-upload {
						margin-left: 20upx;
						width: 100upx;
						height: 100upx;
						border: 2upx solid #ddd;
						font-size: 50upx;
						color: #ccc;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
			}
		}


		input {
			font-size: 28upx;
		}

		.input-placeholder {
			font-size: 28upx;
		}
	}

	.btn-save {
		margin-top: 50upx;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		color: #fff;
		font-size: 32upx;
		background: #DA5650;
		border-radius: 50upx;
		letter-spacing: 10upx;
	}

	.feedback {
		margin-top: 100upx;

		.b-title {
			display: flex;
			align-items: center;
			justify-content: center;

			.iconfont {
				color: #ccc;
			}

			.s-tit {
				margin: 0 20upx;
				font-size: 30upx;
				color: #666;
			}
		}

		.b-items {
			.b-item {
				padding: 40upx 0;
				border-bottom: 2upx solid #eee;

				&:last-child {
					border-bottom: none;
				}
			}
		}

		.b-main {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.s-cont {
				font-size: 28upx;
				color: #333;
			}

			.s-more {
				font-size: 40upx;
				color: #ddd;
			}
		}

		.b-foot {
			margin-top: 30upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.s-info {
				display: flex;
				align-items: center;

				.s-type {
					margin-right: 20upx;
					font-size: 24upx;
					padding: 5upx 15upx;
					color: #fff;
					background: #666;
					border-radius: 10upx;
				}

				.s-state {
					font-size: 26upx;
					color: #666;

					&.f-state-1 {}

					&.f-state-2 {
						color: #DA5650;
						font-weight: bold;
					}
				}
			}

			.s-time {
				font-size: 24upx;
				color: #666;
			}
		}
	}
</style>
